Imports System.Reflection
Imports System.Xml

Partial Public Class _Default
    Inherits System.Web.UI.Page


    Dim strURL As String = "http://139.71.180.72/Cope58Secure/InquireMRAccount.asmx?wsdl"

    Dim objwsdl As Wsdl
    Dim objassembly As Assembly


    Dim objxmlReq As New XmlDocument
    Dim objRootElement As XmlElement
    Dim strXPath As String
    Dim strXML As String

    Dim strRootElementName As String
    Dim arylist As New ArrayList
    Dim RootType As Object


    Dim TypesHashTable As New Hashtable

    Dim strSemiPath As String




    Private Sub updpnlTreeView_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles updpnlTreeView.Load

        Dim strControlName As String
        Dim strParams() As String
        strControlName = Page.Request.Params.Get("__EVENTARGUMENT")
        If IsPostBack Then
            strParams = strControlName.Split("|")
            Select Case strParams(0)
                Case "Go"
                    MethodTree.Nodes.Clear()

                    ClearInputTree()
                    'InputTree.Nodes.Clear()
                    strURL = txtUrl.Text
                    If objwsdl Is Nothing Then
                        GenerateAssembly()
                    End If

                    PopulateMethods()
            End Select

        End If
        


    End Sub


    Sub ClearInputTree()
        Try
            Dim xmldatasource1 As New XmlDataSource
            xmldatasource1.Data = "<Null><Node></Node></Null>"
            xmldatasource1.EnableCaching = False



            InputTree.DataSource = xmldatasource1
            InputTree.DataBind()

            Me.InputTree.Nodes.Clear()
        Catch ex As Exception
        End Try


        'Dim treecoll As TreeNodeCollection
        'treecoll = InputTree.Nodes
        'If treecoll.Count <> 0 Then
        '    InputTree.Nodes.Remove(treecoll.Item(0))
        'End If

    End Sub


    Sub GenerateAssembly()
        'If Session.Contents.Item("wsdl") Is Nothing Then
        'objwsdl.Reset()
        objwsdl = New Wsdl
        'objwsdl.Paths.Add(strURL)
        objwsdl.Paths.Add(txtUrl.Text)
        objwsdl.Generate()
        objassembly = objwsdl.ProxyAssembly

        'Session.Contents.Add("wsdl", objwsdl)
        'End If
    End Sub

    Private Sub updpnlRequest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles updpnlRequest.Load
        Dim strControlName As String
        Dim strParams() As String
        strControlName = Page.Request.Params.Get("__EVENTARGUMENT")
        If IsPostBack Then
            strParams = strControlName.Split("|")
            Select Case strParams(0)
                Case "NodeClick"
                    GenerateAssembly()
                    PopulateInputTree(strParams(1))
                Case "AssignValue"
                    AssignValue(strParams(1), strParams(2))
            End Select

        End If
    End Sub


    Sub AssignValue(ByVal strValue As String, ByVal strXPath As String)



        Dim objreqxml As New XmlDocument
        objreqxml.LoadXml(hdnXML.Value)

        Dim xmldatasource1 As New XmlDataSource
        xmldatasource1.EnableCaching = False
        xmldatasource1.Data = objreqxml.InnerXml


        InputTree.DataSource = xmldatasource1
        InputTree.DataBind()


        Dim i As Integer
        Dim arynodevalues() As String = Split(hdnNodeValues.Value, "|")

        Dim ValuesHashTable As New Hashtable

        hdnNodeValues.Value = ""
        For i = 0 To arynodevalues.Length - 1
            Dim aryvalue() As String = Split(arynodevalues(i), ",")
            If aryvalue.Length > 1 Then
                If Not ValuesHashTable.ContainsKey(aryvalue(0)) Then
                    ValuesHashTable.Add(aryvalue(0), aryvalue(1))
                   
                Else
                    ValuesHashTable.Remove(aryvalue(0))
                    ValuesHashTable.Add(aryvalue(0), aryvalue(1))
                    
                End If
               
            End If
        Next


        Dim objkey As Object
        For Each objkey In ValuesHashTable
            Dim treenode As TreeNode = InputTree.FindNode(objkey.key)
            If objkey.value <> "" Then
                treenode.Text = treenode.Text & "  =  " & objkey.value
            Else
                treenode.Text = treenode.Text
            End If



            Dim objxmlnode As XmlNode = objreqxml.SelectSingleNode(objkey.key)
            objxmlnode.InnerText = objkey.value
            hdnNodeValues.Value = hdnNodeValues.Value + objkey.key + "," + objkey.value + "|"
        Next


       
        hdnXML.Value = objreqxml.InnerXml

    End Sub


    Sub PopulateInputTree(ByVal strMethodname As String)

        InputTree.Nodes.Clear()

        Dim objtypes() As Type = objassembly.GetTypes
        Dim objtype As Type
        Dim objmethodinfo() As MethodInfo

        For Each objtype In objtypes
            If (objtype.BaseType.ToString() = "System.Web.Services.Protocols.SoapHttpClientProtocol") Then
                objmethodinfo = objtype.GetMethods
                Dim objmethod As MethodInfo


                For Each objmethod In objmethodinfo
                    If objmethod.DeclaringType.ToString = objtype.Name Then

                        If (objmethod.Name.StartsWith("begin", StringComparison.OrdinalIgnoreCase)) Or (objmethod.Name.StartsWith("end", StringComparison.OrdinalIgnoreCase)) Then
                            Continue For
                        Else

                            If objmethod.Name = strMethodname Then
                                Dim objparams As ParameterInfo() = objmethod.GetParameters
                                Dim objparam As ParameterInfo
                                Dim objtype3 As Type

                                For Each objparam In objparams
                                    objtype3 = objassembly.GetType(objparam.Name)
                                    PopulateArrayList(objtype3)
                                    GetAllTypes1(objtype3)
                                Next

                            End If
                            
                        End If
                    End If
                Next
            End If
        Next



        Dim xmldatasource1 As New XmlDataSource
        xmldatasource1.EnableCaching = False
        xmldatasource1.Data = objxmlReq.InnerXml


        hdnXML.Value = objxmlReq.InnerXml


        ClearInputTree()

        InputTree.DataSource = xmldatasource1
        InputTree.DataBind()


    End Sub



    Sub GetAllTypes(ByVal objReq As Type)
        Try

        Dim strname As String = objReq.Name.Replace("[]", "ary")


            If RootType Is Nothing Then
                RootType = objReq
                'objRootElement = objxmlReq.CreateElement(objReq.Name)
                objRootElement = objxmlReq.CreateElement(strname)
                objxmlReq.AppendChild(objRootElement)
                strRootElementName = objReq.Name
                strXPath = objReq.Name
            End If

            Dim objfield As FieldInfo
            Dim objfields() As FieldInfo = objReq.GetFields
            For Each objfield In objfields
                Dim objtype As Type = objassembly.GetType(objfield.FieldType.FullName)

                If Not objtype Is Nothing Then
                    'strXPath = strXPath & "/" & objfield.Name
                    strXPath = objfield.DeclaringType.Name & "/" & objfield.Name
                End If

                Dim xmlnode As XmlNode
                Dim xmlnodeprevious As XmlNode
                xmlnode = objxmlReq.CreateNode(XmlNodeType.Element, objfield.Name, objfield.Name, "")
                xmlnodeprevious = objxmlReq.SelectSingleNode(strXPath)
                If xmlnodeprevious Is Nothing Then
                    If objfield.DeclaringType.Name = strRootElementName Then
                        objRootElement.AppendChild(xmlnode)
                    Else
                        Dim xmlnodeparent As XmlNode = objxmlReq.SelectSingleNode("//" & objfield.DeclaringType.Name)
                        xmlnodeparent.AppendChild(xmlnode)
                    End If
                Else
                    xmlnodeprevious.AppendChild(xmlnode)
                End If
                If Not objtype Is Nothing Then
                    GetAllTypes(objtype)
                    'strXPath = ""
                End If
            Next
        Catch ex As Exception

        End Try

    End Sub


    Sub GetAllTypes1(ByVal objReq As Type)
        Try
            If RootType Is Nothing Then
                RootType = objReq
                objRootElement = objxmlReq.CreateElement(objReq.Name)
                'objRootElement = objxmlReq.CreateElement(strname)
                objxmlReq.AppendChild(objRootElement)
                strRootElementName = objReq.Name
                'strXPath = objReq.Name
            End If

            Dim objfield As FieldInfo
            Dim objfields() As FieldInfo = objReq.GetFields

            If objReq.IsArray Then
                Dim strname As String = objReq.Name.Replace("[]", "")
                Dim objtype As Type = objassembly.GetType(strname)

                objfields = objtype.GetFields
            End If



            For Each objfield In objfields
                Dim objtype As Type = objassembly.GetType(objfield.FieldType.FullName)

                strXPath = FormatXpath(objfield)
                Try
                    If Not objtype Is Nothing Then
                        If Not TypesHashTable.ContainsKey(objfield.FieldType.FullName.Replace("[]", "")) Then
                            TypesHashTable.Add(objfield.FieldType.FullName.Replace("[]", ""), objfield.Name.Replace("[]", ""))
                        End If
                    End If

                    Dim xmlnode As XmlNode
                    Dim xmlnodeprevious As XmlNode
                    xmlnode = objxmlReq.CreateNode(XmlNodeType.Element, objfield.Name, objfield.Name, "")

                    xmlnodeprevious = objxmlReq.SelectSingleNode(strXPath)
                    If xmlnodeprevious Is Nothing Then
                        If objfield.DeclaringType.Name = strRootElementName Then
                            objRootElement.AppendChild(xmlnode)
                        Else
                            Dim xmlnodeparent As XmlNode = objxmlReq.SelectSingleNode("//" & TypesHashTable(objfield.DeclaringType.Name))
                            xmlnodeparent.AppendChild(xmlnode)
                        End If
                    Else
                        xmlnodeprevious.AppendChild(xmlnode)
                    End If
                Catch ex As Exception

                End Try
                If Not objtype Is Nothing Then
                    If Not objtype.BaseType.FullName = "System.Enum" Then
                        GetAllTypes1(objtype)
                    End If
                End If
            Next



        Catch ex As Exception

        End Try

    End Sub


    Sub PopulateMethods()

        Dim objtypes() As Type = objassembly.GetTypes
        Dim objtype As Type
        Dim objmethodinfo() As MethodInfo

        Dim strMethod As String = ""
        For Each objtype In objtypes
            If (objtype.BaseType.ToString() = "System.Web.Services.Protocols.SoapHttpClientProtocol") Then
                objmethodinfo = objtype.GetMethods
                Dim objmethod As MethodInfo
                Dim objTypesNode As New TreeNode(objtype.Name)
                For Each objmethod In objmethodinfo
                    If objmethod.DeclaringType.ToString = objtype.Name Then
                        If (objmethod.Name.StartsWith("begin", StringComparison.OrdinalIgnoreCase)) Or (objmethod.Name.StartsWith("end", StringComparison.OrdinalIgnoreCase)) Then
                            Continue For
                        Else
                            Dim objOperationsNode As New TreeNode(objmethod.Name)
                            objTypesNode.NavigateUrl = "javascript:void(0)"
                            objOperationsNode.NavigateUrl = "javascript:fPopulateRequest('" & objmethod.Name & "')"
                            objTypesNode.ChildNodes.Add(objOperationsNode)
                            Dim node As New TreeNode


                        End If
                    End If
                Next
                MethodTree.Nodes.Add(objTypesNode)
            End If
        Next
        MethodTree.ExpandAll()
    End Sub

    Sub PopulateArrayList(ByVal objreq As Type)

        Dim objfield As FieldInfo
        Dim objfields() As FieldInfo = objreq.GetFields

        If objreq.IsArray Then
            Dim strname As String = objreq.Name.Replace("[]", "")
            Dim objtype As Type = objassembly.GetType(strname)
            objfields = objtype.GetFields

        End If

        For Each objfield In objfields
            Dim objtype As Type = objassembly.GetType(objfield.FieldType.FullName)

            If Not objfield.FieldType.FullName = "System.String" Then
                arylist.Add(objfield.DeclaringType.Name & "," & objfield.FieldType.FullName.Replace("[]", ""))
                'filewriter.WriteLine(objfield.DeclaringType.Name & "," & objfield.FieldType.FullName.Replace("[]", ""))
            Else
                arylist.Add(objfield.DeclaringType.Name & "," & objfield.Name)
                'filewriter.WriteLine(objfield.DeclaringType.Name & "," & objfield.Name)
            End If

            If Not objtype Is Nothing Then
                If Not objtype.BaseType.FullName = "System.Enum" Then
                    PopulateArrayList(objtype)
                End If
            End If
        Next


    End Sub


    Function FormatXpath(ByVal objfield As FieldInfo) As String

        Dim strpath As String = ""
        Dim strSeed As String

        Dim strXPath As String = ""
        Dim blnEntry As Boolean = True

        If Not objfield.FieldType.FullName = "System.String" Then
            strSeed = objfield.FieldType.FullName.Replace("[]", "")
        Else
            strSeed = objfield.Name
        End If


        While strSeed <> strRootElementName
            strSeed = GetParent(strSeed, objfield.DeclaringType.Name, blnEntry)
            strpath = strSeed + "/" + strpath

            blnEntry = False
        End While
        strXPath = strpath + objfield.Name
        'aryXpaths.Add(strXPath & objfield.Name)

        Return strXPath

    End Function

    Function GetParent(ByVal strChild As String, ByVal strDeclaringType As String, ByVal blnEntry As Boolean) As String
        Dim i As Integer
        Dim str() As String
        For i = 0 To arylist.Count - 1
            str = Split(arylist(i), ",")

            If blnEntry Then
                If str(0) = strDeclaringType And str(1) = strChild Then
                    Return str(0)
                End If
            Else
                If str(1) = strChild Then
                    Return str(0)
                End If
            End If

        Next
        Return ""
    End Function


    Private Sub InputTree_TreeNodeDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles InputTree.TreeNodeDataBound

        e.Node.NavigateUrl = "javascript:fPopulateFieldValue('" & e.Node.ValuePath & "')"
        ' e.Node.PopulateOnDemand = True

    End Sub

   
  
End Class
